package j.framework.qmd.core;

import j.framework.domain.BaseDomain;
import j.util.DES;
import j.util.StringUtils;
import j.util.jdbc.simple.annotation.Column;
import j.util.jdbc.simple.annotation.Table;
import j.util.json.JsonUtils;

import java.util.Map;

/**
 * 查询操作
 * User: jason
 * Date: 2014/12/24 9:49
 */
@Table(name="sys_query_actions",pkColumns = "queryActionId",ukColumns = "queryId,actionName")
public class QueryOperation  extends BaseDomain {
    /**
     * 获取主键的值
     * @return
     */
    @Override
    public String getId() {
        return queryActionId;
    }

    /**
     * 设置主键
     * @param id
     * @return
     */
    @Override
    public void setId(String id) {
        this.queryActionId = id;
    }

    @Column(name="query_action_id",needDes = true)
    private String queryActionId;

    @Column(name="query_md_id",nullable = false,displayName = "查询")
    private String queryId;

    @Column(name="action_id")
    private String actionId; //对应的操作

    @Column(name="action_name",nullable = false,displayName = "操作名称")
    private String actionName;

    @Column(name="on_click",nullable = false,displayName = "点击事件")
    private String onClick;

    @Column(name="custom_parameters")
    private String customParameters;

    @Column(name="generate_method_name")
    private String generateMethodName;

    @Column(noUsed = true)
    private Map<String,Object> customParametersMap = null;

    public Map<String,Object> getCustomParametersMap(){
        if(StringUtils.isNullOrEmpty(customParameters)){
            return null;
        }
        if(customParametersMap!=null){
            return customParametersMap;
        }
        try {
            customParametersMap = JsonUtils.parseMap(customParameters);
        }catch (Exception e){
            //指定的json参数错误，忽略
            customParameters = null;
            return null;
        }
        //处理des开头的key
        for(Map.Entry<String,Object> entry : customParametersMap.entrySet()){
            if(entry.getKey().startsWith("des")){
                if(entry.getValue() != null){
                    entry.setValue(DES.encrypt(String.valueOf(entry.getValue())));
                }
            }
        }
        return customParametersMap;
    }

    private String note;

    public String getQueryActionId() {
        return queryActionId;
    }
    public void setQueryActionId(String queryActionId) {
        this.queryActionId = queryActionId;
    }

    public String getQueryId() {
        return queryId;
    }
    public void setQueryId(String queryId) {
        this.queryId = queryId;
    }

    public String getActionId() {
        return actionId;
    }
    public void setActionId(String actionId) {
        this.actionId = actionId;
    }

    public String getActionName() {
        return actionName;
    }
    public void setActionName(String actionName) {
        this.actionName = actionName;
    }

    public String getOnClick() {
        return onClick;
    }
    public void setOnClick(String onClick) {
        this.onClick = onClick;
    }

    public String getCustomParameters() {
        return customParameters;
    }
    public void setCustomParameters(String customParameters) {
        this.customParameters = customParameters;
    }

    public String getGenerateMethodName() {
        return generateMethodName;
    }
    public void setGenerateMethodName(String generateMethodName) {
        this.generateMethodName = generateMethodName;
    }

    public String getNote() {
        return note;
    }
    public void setNote(String note) {
        this.note = note;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        QueryOperation that = (QueryOperation) o;

        if (queryActionId != null ? !queryActionId.equals(that.queryActionId) : that.queryActionId != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        return queryActionId != null ? queryActionId.hashCode() : 0;
    }
}
